#include "common.h"
PSUPER                       fssupercluster;
PCLUSTER                     fsdynamic;
PFSMEMORYCONTROL             fscontrol;
VOID FSServer()
{
   MESSAGE message;
   SIGNAL  signal;

   if(VerifyFSValidity()!=0)
    SetupFileSystem();
   SetupFSBufferControl();
   LoadSuperCluster();
  

/*the following code is just for the test.We don't need to care of them */
  //FILEDESCRIPTOR p;
 // p.path="./artist.elf";
 // CreateFile(&p);
 // p.pointer="I am so longly ,however I should have patience.";
  
 // ELFHEADER header;
//   header.e_elfheadersize=sizeof(ELFHEADER);
 //  header.e_programheadernumber=2;
 //  header.e_programheaderoffset=8;
//   p.pointer=(POINTER4B)(&header);
 //  p.howmany.low=sizeof(ELFHEADER);p.howmany.high=0;
  // p.howmany.low=40;p.howmany.high=0;
  // p.where.high=0;p.where.low=0;
  // InsertOpenedFileContent(&p);
 //  CloseOpenedFile(&p); 
  // p.howmany.low=47;p.howmany.high=0;
  // p.where.high=0;p.where.low=0;
  // InsertOpenedFileContent(&p);
//OpenFile(&p);
  // p.pointer="You name me poor guy,hehe^_^";
  // p.howmany.low=28;p.howmany.high=0;p.where.high=0;p.where.low=0;
  // InsertOpenedFileContent(&p);
   
  // CHAR string[48];
   
   
  // p.pointer=(POINTER4B)&string[0];
  // p.howmany.low=47;
  // p.howmany.high=0;
  // p.where.low=0;p.where.high=0;
   //ReadOpenedFile(&p);
   //string[47]='\0';
   //Print("\n\n content:%s\n",string);
 
 // 
   /*the follow code is used for monitor the control block.So we should delete them.*/
   U4B i=0;
   for(;i<fscontrol[0].usrnumber;i++)
   {
      if(fscontrol[i].attribute&0x1)
	  	Print("%X ",i);
   }



   
   while(1)
   {
    if(ReceiveMessage(&message)!=ARTIST_ERROR_OK)continue;
     switch(message.type)
     {
       case FSSERVER_WHOIAM:
       {
          //Print("FSServer begin ...\n");
          break;
       }
       case FSSERVER_CREATEFILE:
       {
	     signal.signal=FSSERVER_CREATEFILE;
		 signal.dt=message.sourcepid;
	     signal.returnvalue=(U4B)CreateFile((PFILEDESCRIPTOR)(message.value0));
		 NotifyWaited(&signal);
         break;
       }
       case FSSERVER_CREATEDIRECTORY:
       {
	     signal.signal=FSSERVER_CREATEDIRECTORY;
		 signal.dt=message.sourcepid;
	     signal.returnvalue=(U4B)CreateDirectory((PFILEDESCRIPTOR)(message.value0));
		 NotifyWaited(&signal);
         break;
       }
	   case FSSERVER_OPENFILE:
	   {
	     signal.signal=FSSERVER_OPENFILE;
		 signal.dt=message.sourcepid;
	     signal.returnvalue=(U4B)OpenFile((PFILEDESCRIPTOR)(message.value0));
		 NotifyWaited(&signal);
	     break;
	   }
	   case FSSERVER_OPENDIRECTORY:
	   { 
         signal.signal=FSSERVER_OPENDIRECTORY;
		 signal.dt=message.sourcepid;
	     signal.returnvalue=(U4B)OpenDirectory((PFILEDESCRIPTOR)(message.value0));
		 NotifyWaited(&signal);
	     break;
	   }
	   case FSSERVER_INSERTFILECONTENT:
	   {
	     signal.signal=FSSERVER_INSERTFILECONTENT;
		 signal.dt=message.sourcepid;
	     signal.returnvalue=(U4B)InsertOpenedFileContent((PFILEDESCRIPTOR)(message.value0));
		 NotifyWaited(&signal);

	     break;
	   }
	   case FSSERVER_DELETEFILECONTENT:
	   {
	     signal.signal=FSSERVER_DELETEFILECONTENT;
		 signal.dt=message.sourcepid;
	     signal.returnvalue=(U4B)DeleteOpenedFileContent((PFILEDESCRIPTOR)(message.value0));
		 NotifyWaited(&signal);
	     break;
	   }
	   case FSSERVER_DELETEDIRECTORY:
	   {
	     signal.signal=FSSERVER_DELETEDIRECTORY;
		 signal.dt=message.sourcepid;
	     signal.returnvalue=(U4B)DeleteOpenedDirectory((PFILEDESCRIPTOR)(message.value0));
		 NotifyWaited(&signal);
	     break;
	   }
	   case FSSERVER_DELETEFILE:
	   {
	     signal.signal=FSSERVER_DELETEFILE;
		 signal.dt=message.sourcepid;
	     signal.returnvalue=(U4B)DeleteOpenedFile((PFILEDESCRIPTOR)(message.value0));
		 NotifyWaited(&signal);
	     break;
	   }
	   case FSSERVER_READDIRECTORY:
	   {
	     signal.signal=FSSERVER_READDIRECTORY;
		 signal.dt=message.sourcepid;
	     signal.returnvalue=(U4B)ReadOpenedDirectory ((PFILEDESCRIPTOR)(message.value0));
		 NotifyWaited(&signal);
	     break;
	   }
	   case FSSERVER_CLOSEFILE:
	   {
	     signal.signal=FSSERVER_CLOSEFILE;
		 signal.dt=message.sourcepid;
	     signal.returnvalue=(U4B)CloseOpenedFile((PFILEDESCRIPTOR)(message.value0));
		 NotifyWaited(&signal);
	     break;
	   }
	   case FSSERVER_CLOSEDIRECTORY:
	   {
	     signal.signal=FSSERVER_CLOSEDIRECTORY;
		 signal.dt=message.sourcepid;
	     signal.returnvalue=(U4B)CloseOpenedDirectory((PFILEDESCRIPTOR)(message.value0));
		 NotifyWaited(&signal);
	     break;
	   }
       case FSSERVER_READFILE:
       {
         signal.signal=FSSERVER_READFILE;
		 signal.dt=message.sourcepid;
	     signal.returnvalue=(U4B)ReadOpenedFile((PFILEDESCRIPTOR)(message.value0));
		 NotifyWaited(&signal);
	     break;
       }
     }
   }
}
